package com.wingsfrog.puzzle.controller;

import com.wingsfrog.puzzle.base.BaseController;
import com.wingsfrog.puzzle.modules.dao.PuzzlesDao;
import com.wingsfrog.puzzle.service.PictureService;
import com.wingsfrog.puzzle.service.PuzzleRecordService;
import com.wingsfrog.puzzle.service.ScoreService;
import com.wingsfrog.puzzle.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "no_one_less")
public class NoOneLessController extends BaseController {
    @Autowired
    PictureService pictureService;
    @Autowired
    PuzzleRecordService recordService;
    @Autowired
    UserService userService;
    @Autowired
    ScoreService scoreService;
    @Autowired
    PuzzlesDao puzzlesDao;

    private Logger logger = Logger.getLogger(NoOneLessController.class);

    @RequestMapping(value="upload_score", method = {RequestMethod.GET})
    @Transactional
    @ResponseBody
    public Object uploadScore(@RequestParam("score") Integer score,
                              HttpServletRequest request) {
        logger.info("uploadScore(score=" + score.toString() + ")");
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        scoreService.getBestScore(userId, 2, score);
        Map<String, Integer> res = new HashMap<>();
        res.put("score", score);
        return score;
    }

    @RequestMapping(value = "",method = {RequestMethod.GET})
    public String index(Model model){
        model.addAttribute("arg","hello world");
        return "noOneLess/index";
    }

}
